"""
Light sources.
"""
__all__ = ["Light"]

from OpenGL.GL import *
from graphicobject import *

# Some globals
defaultLightAmbient = (0,0,0,1)
defaultLightDiffuse = (1,1,1,1)
defaultLightSpecular = (1,1,1,1)
defaultLightPosition = (0,0,1,0)

class Light(object):
    "Represents a positional or directional light source"

    def __init__(self,
                 lightno=0,
                 ambient=defaultLightAmbient,
                 diffuse=defaultLightDiffuse,
                 specular=defaultLightSpecular,
                 position=defaultLightPosition,
                 on=True):
        """Constructor.
        @param lightno: which light source is modeled by this object. May be an
        integer from 0 to 7 or from GL_LIGHT0 to GL_LIGHT7.
        @param ambient: ambient parameters (4-float vector)
        @param specular: specular parameters (4-float vector)
        @param diffuse: diffuse parameters (4-float vector)
        @param position: a point (w=1) for a positional source or a vector (w=0)
        for a directional source (4-float vector)
        @param on: Tells whether this light should be turned on or off (boolean).
        """
        if lightno>=GL_LIGHT0: self.lightno = lightno 
        else: self.lightno = lightno+GL_LIGHT0
        self.ambient = ambient
        self.diffuse = diffuse
        self.specular = specular
        self.position = position
        self.on = on
    
    def draw(self):
        "Renders the light in the scene."
        if self.on:
            glEnable(self.lightno)
            glLightfv(self.lightno,GL_AMBIENT,self.ambient)
            glLightfv(self.lightno,GL_DIFFUSE,self.diffuse)
            glLightfv(self.lightno,GL_SPECULAR,self.specular)
            glLightfv(self.lightno,GL_POSITION,self.position)
        else:
            glDisable(self.lightno)

if __name__=="__main__":
    # A simple test
    light = Light()

